//
//  JBWHomeCell.swift
//  新浪微博
//
//  Created by 季伯文 on 2017/7/4.
//  Copyright © 2017年 bowen. All rights reserved.
//

import UIKit
import SnapKit
class JBWHomeCell: UITableViewCell {

    //记录底部视图的顶部约束 便于卸载赋值
    var bottomViewTopConstraint: Constraint?
    
    //提供一个模型属性 供外界赋值
    var statusViewModel: JBWStatusViewModel?{
        didSet{
            originalView.statusViewModel = statusViewModel
            
            //卸载约束
            bottomViewTopConstraint?.deactivate()
            
            //判断是否有转发微博
            if let _ = statusViewModel?.homeModel?.retweeted_status {
                //赋值
                retweetView.statusViewModel = statusViewModel
                bottomView.snp.makeConstraints({ (make) in
                    bottomViewTopConstraint = make.top.equalTo(retweetView.snp.bottom).constraint
                })
                //显示转发微博
                retweetView.isHidden = false
            } else {
                
                bottomView.snp.makeConstraints({ (make) in
                    bottomViewTopConstraint = make.top.equalTo(originalView.snp.bottom).constraint
                })
                //隐藏转发微博
                retweetView.isHidden = true
            }
        }
    }

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }


    //设置视图
    private func setupUI(){
        backgroundColor = RGB(r: 235, g: 235, b: 235)
        contentView.addSubview(originalView)
        contentView.addSubview(retweetView)
        contentView.addSubview(bottomView)
        
        //约束
        originalView.snp.makeConstraints { (make) in
            make.left.right.equalTo(contentView)
            make.top.equalTo(8)
        }
        retweetView.snp.makeConstraints { (make) in
            make.left.right.equalTo(contentView)
            make.top.equalTo(originalView.snp.bottom)
        }
        bottomView.snp.makeConstraints { (make) in
            make.left.right.equalTo(contentView)
            bottomViewTopConstraint = make.top.equalTo(retweetView.snp.bottom).constraint
            make.height.equalTo(35)
            make.bottom.equalTo(contentView)
        }
        
    }

    //懒加载控件
    //原创微博
    private lazy var originalView: JBWHomeOriginalView = JBWHomeOriginalView()
    //转发微博
    private lazy var retweetView: JBWHomeRetweetView = JBWHomeRetweetView()
    //底部视图
    private lazy var bottomView: JBWHomeBottomView = JBWHomeBottomView()






}
